/*
 * TraceabilityTreeItem.java
 *
 * $Id: TraceabilityTreeItem.java,v 1.3 2009-03-02 15:31:23 mario Exp $
 */
package org.ceteca.explica.client.traceability.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import org.ceteca.explica.core.traceability.InterfaceTraceabilityTree;

import org.ceteca.explica.client.traceability.gui.TraceabilityTreeItem;
import org.ceteca.explica.client.traceability.gui.TraceabilityTreeView;
import org.ceteca.javaglade.GenericListener;
import org.ceteca.javaglade.toolkit.swt.SwtToolkit;
import org.ceteca.explica.core.traceability.tree.AppccInfo;
import org.ceteca.explica.core.traceability.tree.ProcessInfo;
import org.ceteca.explica.core.traceability.tree.ProductInfo;
import org.ceteca.explica.core.traceability.tree.PropertyInfo;
import org.ceteca.explica.core.traceability.tree.PurchaseInfo;
import org.ceteca.explica.core.traceability.tree.WarehouseInfo;
import org.ceteca.explica.core.traceability.tree.WorkcenterInfo;
import org.ceteca.explica.core.traceability.tree.WorkerInfo;
import org.ceteca.explica.core.util.DateTime;

/**
 * GUI class that implements a Traceability Tree Item. 
 * It extends the TreeItem GUI Object to add extra data as the Info
 * object linked to the TreeItem or the TreeItem name.
 * <br/>
 * Responsibilities:
 * <ul>
 *  <li>Define traceability item types.</li>
 * 	<li>Store the tree item info.</li>
 *  <li>Build the GUI for any traceability tree item.</li>
 *  <li>Tree item conversion to String or XML format.</li>
 * </ul>
 * <br/>
 * @author Mario García García <mario@imagos.es>
 * Copyright (c) 2007 Fundación Centro Tecnolóxico da Carne
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * <br/>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * <br/>
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class TraceabilityTreeItem {
	/** 
	 * Constant definition with the item type UNDEFINED. It's the default
	 * value to point out that no item type has been selected.
	 */
	public static final int NODE_UNDEFINED = 0x00;
	/**
	 * Constant definition with the item type PRODUCT.
	 */
	public static final int NODE_PRODUCT = 0x01;
	/**
	 * Constant definition with the item type PROCESS.
	 */
	public static final int NODE_PROCESS = 0x02;
	/**
	 * Constant definition with the item type WORKER.
	 */
	public static final int NODE_WORKER = 0x03;
	/**
	 * Constant definition with the item type WORKCENTER.
	 */
	public static final int NODE_WORKCENTER = 0x04;
	/**
	 * Constant definition with the item type WAREHOUSE.
	 */
	public static final int NODE_WAREHOUSE = 0x05;
	/**
	 * Constant definition with the item type PURCHASE.
	 */
	public static final int NODE_PURCHASE = 0x06;
	/**
	 * Constant definition with the item type PROPERTY.
	 */
	public static final int NODE_PROPERTY = 0x07;
	/**
	 * Constant definition with the item type APPCC.
	 */
	public static final int NODE_APPCC = 0x08;
	/**
	 * Constant definition with the item type COMMENT.
	 */
	public static final int NODE_COMMENT = 0x09;
	
	/** Traceability tree item icon file path. */
	private String iconFile;
	/** Traceability tree item type. */
	private int type;
	/** Traceability tree item data object. */
	private Object info;
	/** SWT Toolkit used to build the tree item GUI. */
	private SwtToolkit swtToolkit;
	/** Traceability tree item widget. */
	private TreeItem itemWidget;
	/** Traceability tree item name. */
	private String name;
	
	/**
	 * Constructor for a root traceability tree item. It's used
	 * when the item created is the main product of the traceability
	 * file.
	 * @param swtToolkit SwtToolkit, toolkit instance used to build the 
	 * GUI for the tree items.
	 * @param parent Tree, Tree widget. 
	 * @param style int, SWT constant with the style configuration 
	 * for the item. By default it's SWT.NONE.
	 * @param info Object, info object with the data linked to the tree item.
	 * @param text String, text corresponding to the tree item,
	 * that will be showed on screen.
	 * @param iconFile String, path of the icon for the tree item.
	 * @see org.eclipse.swt.SWT
	 */
	public TraceabilityTreeItem(SwtToolkit swtToolkit, Tree parent, int style, Object info, String text, String iconFile) {
		// Init the common values of the TraceabilityTreeItem
		this.initTraceabilityTreeItem(swtToolkit, style, info, text, iconFile);
		
		// Create the widget TreeItem
		this.setItemWidget((TreeItem)this.swtToolkit.treeItem(parent, this.getName(), text, iconFile, -1));
	}
	
	/**
	 * Constructor for a root traceability tree item. It's used
	 * when the item created is the main product of the traceability
	 * file.
	 * @param swtToolkit SwtToolkit, toolkit instance used to build the 
	 * GUI for the tree items.
	 * @param parent Tree, Tree widget. 
	 * @param style int, SWT constant with the style configuration 
	 * for the item. By default it's SWT.NONE.
	 * @param info Object, info object with the data linked to the tree item.
	 * @param text String, text corresponding to the tree item,
	 * that will be showed on screen.
	 * @param iconFile String, path of the icon for the tree item.
	 * @param index int, index position at which the item will be inserted.
	 * @see org.eclipse.swt.SWT
	 */
	public TraceabilityTreeItem(SwtToolkit swtToolkit, Tree parent, int style, Object info, String text, String iconFile, int index) {
		// Init the common values of the TraceabilityTreeItem
		this.initTraceabilityTreeItem(swtToolkit, style, info, text, iconFile);
		
		// Create the widget TreeItem
		this.setItemWidget((TreeItem)this.swtToolkit.treeItem(parent, this.getName(), text, iconFile, index));
	}

	/**
	 * Constructor for a non-root traceability tree item. It's used
	 * when the item created is any but the main product item of the
	 * traceability file.
	 * @param swtToolkit SwtToolkit, toolkit instance used to build the 
	 * GUI for the tree items.
	 * @param parent TraceabilityTreeItem, parent item. 
	 * @param style int, SWT constant with the style configuration 
	 * for the item. By default it's SWT.NONE.
	 * @param info Object, info object with the data linked to the tree item.
	 * @param text String, text corresponding to the tree item,
	 * that will be showed on screen.
	 * @param iconFile String, path of the icon for the tree item.
	 * @see org.eclipse.swt.SWT
	 */
	public TraceabilityTreeItem(SwtToolkit swtToolkit, TraceabilityTreeItem parent, int style, Object info, String text, String iconFile) {
		// Init the common values of the TraceabilityTreeItem
		this.initTraceabilityTreeItem(swtToolkit, style, info, text, iconFile);
		
		// Create the widget TreeItem
		this.setItemWidget((TreeItem)this.swtToolkit.treeItem(parent.getItemWidget(), this.getName(), text, iconFile, -1));
	}

	/**
	 * Constructor for a non-root traceability tree item. It's used
	 * when the item created is any but the main product item of the
	 * traceability file.
	 * @param swtToolkit SwtToolkit, toolkit instance used to build the 
	 * GUI for the tree items.
	 * @param parent TraceabilityTreeItem, parent item. 
	 * @param style int, SWT constant with the style configuration 
	 * for the item. By default it's SWT.NONE.
	 * @param info Object, info object with the data linked to the tree item.
	 * @param text String, text corresponding to the tree item,
	 * that will be showed on screen.
	 * @param iconFile String, path of the icon for the tree item.
	 * @param index int, index position at which the item will be inserted.
	 * @see org.eclipse.swt.SWT
	 */
	public TraceabilityTreeItem(SwtToolkit swtToolkit, TraceabilityTreeItem parent, int style, Object info, String text, String iconFile, int index) {
		// Init the common values of the TraceabilityTreeItem
		this.initTraceabilityTreeItem(swtToolkit, style, info, text, iconFile);
		
		// Create the widget TreeItem
		this.setItemWidget((TreeItem)this.swtToolkit.treeItem(parent.getItemWidget(), this.getName(), text, iconFile, index));
	}
	
	/**
	 * Constructor for a root traceability tree item. It's used
	 * when the item created is the main product item of the
	 * traceability file. It also adds an item selection event handler.
	 * @param swtToolkit SwtToolkit, toolkit instance used to build the 
	 * GUI for the tree items.
	 * @param parent Tree, Tree widget. 
	 * @param style int, SWT constant with the style configuration 
	 * for the item. By default it's SWT.NONE.
	 * @param info Object, info object with the data linked to the tree item.
	 * @param text String, text corresponding to the tree item,
	 * that will be showed on screen.
	 * @param iconFile String, path of the icon for the tree item.
	 * @param viewer TraceabilityTreeViewer, event handler object.
	 * @param handlerName String, event handler method.
	 * @see org.eclipse.swt.SWT
	 */
	public TraceabilityTreeItem(SwtToolkit swtToolkit, Tree parent, int style, Object info, String text, String iconFile, TraceabilityTreeView viewer, String handlerName) {
		// Init the common values of the TraceabilityTreeItem
		this.initTraceabilityTreeItem(swtToolkit, style, info, text, iconFile);
		
		// Create the widget TreeItem
		this.setItemWidget((TreeItem)this.swtToolkit.treeItem(parent, this.getName(), text, iconFile, -1));
		
		// Add the handler of the selection event
		this.getItemWidget().addListener(SWT.Selection, new GenericListener(handlerName, viewer));
	}
	
	/**
	 * Constructor for a root traceability tree item. It's used
	 * when the item created is the main product item of the
	 * traceability file. It also adds an item selection event handler.
	 * @param swtToolkit SwtToolkit, toolkit instance used to build the 
	 * GUI for the tree items.
	 * @param parent Tree, Tree widget. 
	 * @param style int, SWT constant with the style configuration 
	 * for the item. By default it's SWT.NONE.
	 * @param info Object, info object with the data linked to the tree item.
	 * @param text String, text corresponding to the tree item,
	 * that will be showed on screen.
	 * @param iconFile String, path of the icon for the tree item.
	 * @param viewer TraceabilityTreeViewer, event handler object.
	 * @param handlerName String, event handler method.
	 * @param index int, index position at which the item will be inserted.
	 * @see org.eclipse.swt.SWT
	 */
	public TraceabilityTreeItem(SwtToolkit swtToolkit, Tree parent, int style, Object info, String text, String iconFile, TraceabilityTreeView viewer, String handlerName, int index) {
		// Init the common values of the TraceabilityTreeItem
		this.initTraceabilityTreeItem(swtToolkit, style, info, text, iconFile);
		
		// Create the widget TreeItem
		this.setItemWidget((TreeItem)this.swtToolkit.treeItem(parent, this.getName(), text, iconFile, index));
		
		// Add the handler of the selection event
		this.getItemWidget().addListener(SWT.Selection, new GenericListener(handlerName, viewer));
	}
	
	/**
	 * Constructor for a non-root traceability tree item. It's used
	 * when the item created is any but the main product item of the
	 * traceability file.
	 * @param swtToolkit SwtToolkit, toolkit instance used to build the 
	 * GUI for the tree items.
	 * @param parent TraceabilityTreeItem, parent item. 
	 * @param style int, SWT constant with the style configuration 
	 * for the item. By default it's SWT.NONE.
	 * @param info Object, info object with the data linked to the tree item.
	 * @param text String, text corresponding to the tree item,
	 * that will be showed on screen.
	 * @param iconFile String, path of the icon for the tree item.
	 * @param viewer TraceabilityTreeViewer, event handler object.
	 * @param handlerName String, event handler method.
	 * @see org.eclipse.swt.SWT
	 */
	public TraceabilityTreeItem(SwtToolkit swtToolkit, TraceabilityTreeItem parent, int style, Object info, String text, String iconFile, TraceabilityTreeView viewer, String handlerName) {
		// Init the common values of the TraceabilityTreeItem
		this.initTraceabilityTreeItem(swtToolkit, style, info, text, iconFile);
		
		// Create the widget TreeItem
		this.setItemWidget((TreeItem)this.swtToolkit.treeItem(parent.getItemWidget(), this.getName(), text, iconFile, -1));
		
		// Add the handler of the selection event
		this.getItemWidget().addListener(SWT.Selection, new GenericListener(handlerName, viewer));
	}

	/**
	 * Constructor for a non-root traceability tree item. It's used
	 * when the item created is any but the main product item of the
	 * traceability file.
	 * @param swtToolkit SwtToolkit, toolkit instance used to build the 
	 * GUI for the tree items.
	 * @param parent TraceabilityTreeItem, parent item. 
	 * @param style int, SWT constant with the style configuration 
	 * for the item. By default it's SWT.NONE.
	 * @param info Object, info object with the data linked to the tree item.
	 * @param text String, text corresponding to the tree item,
	 * that will be showed on screen.
	 * @param iconFile String, path of the icon for the tree item.
	 * @param viewer TraceabilityTreeViewer, event handler object.
	 * @param handlerName String, event handler method.
	 * @param index int, index position at which the item will be inserted.
	 * @see org.eclipse.swt.SWT
	 */
	public TraceabilityTreeItem(SwtToolkit swtToolkit, TraceabilityTreeItem parent, int style, Object info, String text, String iconFile, TraceabilityTreeView viewer, String handlerName, int index) {
		// Init the common values of the TraceabilityTreeItem
		this.initTraceabilityTreeItem(swtToolkit, style, info, text, iconFile);

		System.out.println("Añadiendo tree item: " + text + " al parent: " + parent + " [" + parent.getItemWidget().getItemCount() + "]" + " en la posicion: " + index);
		
		// Create the widget TreeItem
		this.setItemWidget((TreeItem)this.swtToolkit.treeItem(parent.getItemWidget(), this.getName(), text, iconFile, index));
		
		// Add the handler of the selection event
		this.getItemWidget().addListener(SWT.Selection, new GenericListener(handlerName, viewer));
	}
	
	/**
	 * Initializes the traceability tree item, setting up all the item
	 * data. 
	 * @param swtToolkit SwtToolkit, toolkit instance used to build the 
	 * GUI for the tree items.
	 * @param style int, SWT constant with the style configuration 
	 * for the item. By default it's SWT.NONE.
	 * @param info Object, info object with the data linked to the tree item.
	 * @param text String, text corresponding to the tree item,
	 * that will be showed on screen.
	 * @param iconFile String, path of the icon for the tree item.
	 * @see org.eclipse.swt.SWT
	 */
	private void initTraceabilityTreeItem(SwtToolkit swtToolkit, int style, Object info, String text, String iconFile) {
		this.swtToolkit = swtToolkit;
		
		
		String widgetName = "treeItem";
		this.setIconFile(iconFile);
		this.setInfo(info);
		if (info == null)
			this.setType(NODE_COMMENT);
		else if (info instanceof ProductInfo) {
			this.setType(NODE_PRODUCT);
			widgetName += "_product_" + ((ProductInfo)info).getReference();
		}
		else if (info instanceof ProcessInfo) {
			this.setType(NODE_PROCESS);
			widgetName += "_process_" + ((ProcessInfo)info).getCodOperation();
		}
		else if (info instanceof WorkerInfo) {
			this.setType(NODE_WORKER);
			widgetName += "_worker_" + ((WorkerInfo)info).getCodWorker();
		}
		else if (info instanceof WorkcenterInfo) {
			this.setType(NODE_WORKCENTER);
			widgetName += "_workcenter_" + ((WorkcenterInfo)info).getCodWorkcenter();
		}
		else if (info instanceof WarehouseInfo) {
			this.setType(NODE_WAREHOUSE);
			widgetName += "_warehouse_" + ((WarehouseInfo)info).getCodWarehouse();
		}
		else if (info instanceof PurchaseInfo) {
			this.setType(NODE_PURCHASE);
			widgetName += "_purchase_" + ((PurchaseInfo)info).getCodPurchaseConfirmation();
		}
		else if (info instanceof PropertyInfo) {
			this.setType(NODE_PROPERTY);
			widgetName += "_prop_" + ((PropertyInfo)info).getCod();
		}
		else if (info instanceof AppccInfo) {
			this.setType(NODE_APPCC);
//			widgetName += "_appcc_" + ((WorkerInfo)info).getCodWorker();
		}
		
		// Set up the widget's name
		this.setName(widgetName);
	}

	/**
	 * Gets the traceability tree item icon file path.
	 * @return String, item icon file path.
	 */
	public String getIconFile() {
		return iconFile;
	}

	/**
	 * Sets up the traceability tree item icon file path. 
	 * @param iconFile String, an icon file path.
	 */
	public void setIconFile(String iconFile) {
		this.iconFile = iconFile;
	}

	/**
	 * Gets the traceability tree item data object.
	 * @return Object, item data object.
	 */
	public Object getInfo() {
		return info;
	}

	/**
	 * Sets up the traceability tree item data object. 
	 * @param info Object, a data object.
	 */
	public void setInfo(Object info) {
		this.info = info;
	}
	
	/**
	 * Updates the traceability tree item data object. Sets up
	 * all the data (of the corresponding item type) kept in the 
	 * tree item data object. That means that it will overwrite only
	 * the object data fields of the item type level.
	 * @param info Object, a data object.
	 */
	public void updateInfo(Object info) {
		if (this.info != null) {
			switch (this.type) {
				case TraceabilityTreeItem.NODE_PRODUCT:
					((ProductInfo)this.info).update((ProductInfo)info);
					break;
				case TraceabilityTreeItem.NODE_PROPERTY:
					((PropertyInfo)this.info).update((PropertyInfo)info);
					break;
				case TraceabilityTreeItem.NODE_PURCHASE:
					((PurchaseInfo)this.info).update((PurchaseInfo)info);
					break;
				case TraceabilityTreeItem.NODE_PROCESS:
					((ProcessInfo)this.info).update((ProcessInfo)info);
					break;
				case TraceabilityTreeItem.NODE_WORKER:
					((WorkerInfo)this.info).update((WorkerInfo)info);
					break;
				case TraceabilityTreeItem.NODE_WORKCENTER:
					((WorkcenterInfo)this.info).update((WorkcenterInfo)info);
					break;
				case TraceabilityTreeItem.NODE_WAREHOUSE:
					((WarehouseInfo)this.info).update((WarehouseInfo)info);
					break;
				case TraceabilityTreeItem.NODE_APPCC:
//					((AppccInfo)this.info).update((AppccInfo)info);
					break;
				case TraceabilityTreeItem.NODE_COMMENT:
					this.info = info;
					break;
			}
		}
		else {
			this.setInfo(info);
		}
	}

	/**
	 * Gets the traceability tree item type.
	 * @return int, item type.
	 */
	public int getType() {
		return type;
	}

	/**
	 * Sets up the traceability tree item type. 
	 * @param type int, an item type.
	 */
	public void setType(int type) {
		this.type = type;
	}

	/**
	 * Gets the traceability tree item widget.
	 * @return TreeItem, item widget.
	 */
	public TreeItem getItemWidget() {
		return itemWidget;
	}

	/**
	 * Sets up the traceability tree item widget. 
	 * @param itemWidget TreeItem, an item widget.
	 */
	public void setItemWidget(TreeItem itemWidget) {
		this.itemWidget = itemWidget;
	}

	/**
	 * Gets the traceability tree item name.
	 * @return String, item name.
	 */
	public String getName() {
		return name;
	}

	/**
	 * Sets up the traceability tree item name. 
	 * @param name String, a name.
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	/**
	 * Converts the traceability tree item to a formatted String
	 * that show all the item's data.
	 * @return String, formatted String with item's data.
	 */
	public String toString() {
		StringBuffer result = new StringBuffer();
		
		result.append("\n-.-.-.-.-.-.-.-.-.-.-.-.-");
		result.append("\nTREE_ITEM: ");
		result.append(this.name);
		result.append(" (");
		result.append(this.type);
		result.append(") ");
		result.append("\nICON = ");
		result.append(this.iconFile);
		result.append("\nINFO = ");
		result.append(this.info==null?"null":this.info.getClass());
		result.append("\nITEM = ");
		result.append(this.itemWidget);
		result.append("\n-.-.-.-.-.-.-.-.-.-.-.-.-");
		
		return result.toString();
	}
	
	/**
	 * Converts the traceability tree item to a formatted String
	 * in XML format, that will hold all the item's data.
	 * @param prefix String, prefix added to each of the XML lines
	 * resulting from the item conversion.
	 * @return String, formatted String with the item's data in XML
	 * format.
	 */
	public String toXml(String prefix) {
		String result = null;

		switch (this.getType()) {
			case NODE_PRODUCT:
				result = this.productToXml((ProductInfo)this.getInfo(), prefix);
				break;
			case NODE_PROPERTY:
				result = this.propertyToXml((PropertyInfo)this.getInfo(), prefix);
				break;
			case NODE_PURCHASE:
				result = this.purchaseToXml((PurchaseInfo)this.getInfo(), prefix);
				break;
			case NODE_PROCESS:
				result = this.processToXml((ProcessInfo)this.getInfo(), prefix);
				break;
			case NODE_WORKER:
				result = this.workerToXml((WorkerInfo)this.getInfo(), prefix);
				break;
			case NODE_WORKCENTER:
				result = this.workcenterToXml((WorkcenterInfo)this.getInfo(), prefix);
				break;
			case NODE_WAREHOUSE:
				result = this.warehouseToXml((WarehouseInfo)this.getInfo(), prefix);
				break;
			case NODE_APPCC:
//				result = this.appccToXml((AppccInfo)this.getInfo(), prefix);
				break;
			case NODE_COMMENT:
				result = this.commentToXml((String)this.getInfo(), prefix);
				break;
		} // switch
		
		return result;
	}
	
	/**
	 * Converts the traceability tree product item to a formatted String
	 * in XML format, that will hold all the item's data.
	 * @param i ProductInfo, info object linked to the item.
	 * @param prefix String, prefix added to each of the XML lines
	 * resulting from the item conversion.
	 * @return String, formatted String with the product item's data in XML
	 * format.
	 */
	private String productToXml(ProductInfo i, String prefix) {
		StringBuffer result = new StringBuffer();

		// Append product tag
		result.append(prefix);
		result.append(InterfaceTraceabilityTree.START_XML_TAG);
		result.append(InterfaceTraceabilityTree.PRODUCT_TAG);
		result.append(InterfaceTraceabilityTree.WHITE_SPACE);
		
		// Append product set number attribute
		result.append(InterfaceTraceabilityTree.PROPERTY_PRODUCT_SETNUMBER_PROPERTYNAME);
		result.append("=\"");
		result.append(i.getSetNumber());
		result.append("\"");
		result.append(InterfaceTraceabilityTree.WHITE_SPACE);
		
		// Append product description attribute
		result.append(InterfaceTraceabilityTree.PROPERTY_PRODUCT_DESCRIPTION_PROPERTYNAME);
		result.append("=\"");
		result.append(i.getDescription());
		result.append("\"");
		result.append(InterfaceTraceabilityTree.WHITE_SPACE);
		
		// Append product barcode attribute
		result.append(InterfaceTraceabilityTree.PROPERTY_PRODUCT_BARCODE_PROPERTYNAME);
		result.append("=\"");
		result.append(i.getBarcode());
		result.append("\"");
		result.append(InterfaceTraceabilityTree.WHITE_SPACE);
		
		// Append product reference attribute
		result.append(InterfaceTraceabilityTree.PROPERTY_PRODUCT_REFERENCE_PROPERTYNAME);
		result.append("=\"");
		result.append(i.getReference());
		result.append("\"");
		result.append(InterfaceTraceabilityTree.WHITE_SPACE);
		
		// Append product in date attribute
		DateTime dt = new DateTime();
		dt.setDateFormat(InterfaceTraceabilityTree.CONST_SHORT_DATE_PATTERN);
		dt.setDate(i.getDateIn());
		result.append(InterfaceTraceabilityTree.PROPERTY_PRODUCT_DATEIN_PROPERTYNAME);
		result.append("=\"");
		result.append(dt.getDateAsString());
		result.append("\"");
		result.append(InterfaceTraceabilityTree.WHITE_SPACE);
		
		// Append product expiry date attribute
		dt = new DateTime();
		dt.setDateFormat(InterfaceTraceabilityTree.CONST_SHORT_DATE_PATTERN);
		dt.setDate(i.getDateExpiry());
		result.append(InterfaceTraceabilityTree.PROPERTY_PRODUCT_DATEEXPIRY_PROPERTYNAME);
		result.append("=\"");
		result.append(dt.getDateAsString());
		result.append("\"");
		result.append(InterfaceTraceabilityTree.WHITE_SPACE);
		
		// Append product traceability filename attribute
		result.append(InterfaceTraceabilityTree.PROPERTY_PRODUCT_TRACEABILITYFILE_PROPERTYNAME);
		result.append("=\"");
		result.append(i.getTraceabilityFile());
		result.append("\"");

		// Append the end of the tag
		result.append(InterfaceTraceabilityTree.END_XML_TAG);
		result.append(InterfaceTraceabilityTree.END_LINE);
		
		// Append the source starting tag
		if (i.getSourceType() != ProductInfo.SOURCE_NONE) {
			result.append(prefix + InterfaceTraceabilityTree.TAB);
			result.append(InterfaceTraceabilityTree.START_XML_TAG);
			result.append(InterfaceTraceabilityTree.SOURCE_TAG);
			result.append(InterfaceTraceabilityTree.END_XML_TAG);
			result.append(InterfaceTraceabilityTree.END_LINE);
		}
		
		switch (i.getSourceType()) {
			case ProductInfo.SOURCE_PURCHASE:
				result.append(this.purchaseToXml(i.getSourcePurchase(), prefix + InterfaceTraceabilityTree.TAB + InterfaceTraceabilityTree.TAB));
				result.append(InterfaceTraceabilityTree.END_LINE);
				break;
			case ProductInfo.SOURCE_PROCESS:
				result.append(this.processToXml(i.getSourceProcess(), prefix + InterfaceTraceabilityTree.TAB + InterfaceTraceabilityTree.TAB));
				result.append(InterfaceTraceabilityTree.END_LINE);
				break;
		}
		
		// Append the source ending tag
		if (i.getSourceType() != ProductInfo.SOURCE_NONE) {
			result.append(prefix + InterfaceTraceabilityTree.TAB);
			result.append(InterfaceTraceabilityTree.START_XML_TAG);
			result.append(InterfaceTraceabilityTree.SLASH);
			result.append(InterfaceTraceabilityTree.SOURCE_TAG);
			result.append(InterfaceTraceabilityTree.END_XML_TAG);
			result.append(InterfaceTraceabilityTree.END_LINE);
		}
		
		// Append the properties
		if (i.getLProperties() != null && i.getLProperties().size() > 0) {
			for (int index = 0; index < i.getLProperties().size(); index++) {
				result.append(this.propertyToXml((PropertyInfo)i.getLProperties().get(index), prefix + InterfaceTraceabilityTree.TAB));
				result.append(InterfaceTraceabilityTree.END_LINE);
			} // for each property
		} // if exists any property
		
		// Append the warehouses
		if (i.getLWarehouses() != null && i.getLWarehouses().size() > 0) {
			for (int index = 0; index < i.getLWarehouses().size(); index++) {
				result.append(this.warehouseToXml((WarehouseInfo)i.getLWarehouses().get(index), prefix + InterfaceTraceabilityTree.TAB));
				result.append(InterfaceTraceabilityTree.END_LINE);
			} // for each warehouse
		} // if exists any warehouse
		
		// Append the comments
		if (i.getLComments() != null && i.getLComments().size() > 0) {
			for (int index = 0; index < i.getLComments().size(); index++) {
				result.append(this.commentToXml((String)i.getLComments().get(index), prefix + InterfaceTraceabilityTree.TAB));
				result.append(InterfaceTraceabilityTree.END_LINE);
			} // for each comment
		} // if exists any comment
		
		// Append the end tag
		result.append(prefix);
		result.append(InterfaceTraceabilityTree.START_XML_TAG);
		result.append(InterfaceTraceabilityTree.SLASH);
		result.append(InterfaceTraceabilityTree.PRODUCT_TAG);
		result.append(InterfaceTraceabilityTree.END_XML_TAG);
		
		return result.toString();
	}
	
	/**
	 * Converts the traceability tree property item to a formatted String
	 * in XML format, that will hold all the item's data.
	 * @param i PropertyInfo, info object linked to the item.
	 * @param prefix String, prefix added to each of the XML lines
	 * resulting from the item conversion.
	 * @return String, formatted String with the property item's data in XML
	 * format.
	 */
	private String propertyToXml(PropertyInfo i, String prefix) {
		StringBuffer result = new StringBuffer();

		// Append property tag
		result.append(prefix);
		result.append(InterfaceTraceabilityTree.START_XML_TAG);
		result.append(InterfaceTraceabilityTree.PROPERTY_TAG);
		result.append(InterfaceTraceabilityTree.WHITE_SPACE);
		
		// Append property code attribute
		result.append(InterfaceTraceabilityTree.PROPERTY_PROPERTY_CODE_PROPERTYNAME);
		result.append("=\"");
		result.append(i.getCod());
		result.append("\"");
		result.append(InterfaceTraceabilityTree.WHITE_SPACE);
		
		// Append property value attribute
		result.append(InterfaceTraceabilityTree.PROPERTY_PROPERTY_VALUE_PROPERTYNAME);
		result.append("=\"");
		result.append(i.getVal());
		result.append("\"");
		result.append(InterfaceTraceabilityTree.WHITE_SPACE);
		
		// Append property type attribute
		result.append(InterfaceTraceabilityTree.PROPERTY_PROPERTY_TYPE_PROPERTYNAME);
		result.append("=\"");
		result.append(i.getType());
		result.append("\"");
		result.append(InterfaceTraceabilityTree.WHITE_SPACE);
		
		// Append property description attribute
		result.append(InterfaceTraceabilityTree.PROPERTY_PROPERTY_DESCRIPTION_PROPERTYNAME);
		result.append("=\"");
		result.append(i.getDescription());
		result.append("\"");
		result.append(InterfaceTraceabilityTree.WHITE_SPACE);
		
		// Append property format pattern attribute
		result.append(InterfaceTraceabilityTree.PROPERTY_PROPERTY_PATTERN_PROPERTYNAME);
		result.append("=\"");
		result.append(i.getPattern());
		result.append("\"");

		// Append the end of the tag
		result.append(InterfaceTraceabilityTree.SLASH);
		result.append(InterfaceTraceabilityTree.END_XML_TAG);
		
		return result.toString();
	}
	
	/**
	 * Converts the traceability tree purchase item to a formatted String
	 * in XML format, that will hold all the item's data.
	 * @param i PurchaseInfo, info object linked to the item.
	 * @param prefix String, prefix added to each of the XML lines
	 * resulting from the item conversion.
	 * @return String, formatted String with the purchase item's data in XML
	 * format.
	 */
	private String purchaseToXml(PurchaseInfo i, String prefix) {
		StringBuffer result = new StringBuffer();

		// Append purchase tag
		result.append(prefix);
		result.append(InterfaceTraceabilityTree.START_XML_TAG);
		result.append(InterfaceTraceabilityTree.PURCHASE_TAG);
		result.append(InterfaceTraceabilityTree.WHITE_SPACE);
		
		// Append purchase code attribute
		result.append(InterfaceTraceabilityTree.PROPERTY_PURCHASE_PURCHASECONFIRMATION_PROPERTYNAME);
		result.append("=\"");
		result.append(i.getCodPurchaseConfirmation());
		result.append("\"");
		result.append(InterfaceTraceabilityTree.WHITE_SPACE);
		
		// Append purchase supplier attribute
		result.append(InterfaceTraceabilityTree.PROPERTY_PURCHASE_SUPPLIER_PROPERTYNAME);
		result.append("=\"");
		result.append(i.getCodSupplier());
		result.append("\"");

		// Append the end of the tag
		result.append(InterfaceTraceabilityTree.SLASH);
		result.append(InterfaceTraceabilityTree.END_XML_TAG);
		
		return result.toString();
	}

	/**
	 * Converts the traceability tree process item to a formatted String
	 * in XML format, that will hold all the item's data.
	 * @param i ProcessInfo, info object linked to the item.
	 * @param prefix String, prefix added to each of the XML lines
	 * resulting from the item conversion.
	 * @return String, formatted String with the process item's data in XML
	 * format.
	 */
	private String processToXml(ProcessInfo i, String prefix) {
		StringBuffer result = new StringBuffer();

		// Append process tag
		result.append(prefix);
		result.append(InterfaceTraceabilityTree.START_XML_TAG);
		result.append(InterfaceTraceabilityTree.PROCESS_TAG);
		result.append(InterfaceTraceabilityTree.WHITE_SPACE);
		
		// Append process code attribute
		result.append(InterfaceTraceabilityTree.PROPERTY_PROCESS_PROCESSCODE_PROPERTYNAME);
		result.append("=\"");
		result.append(i.getCodOperation());
		result.append("\"");
		result.append(InterfaceTraceabilityTree.WHITE_SPACE);
		
		// Append process date attribute
		DateTime dt = new DateTime();
		dt.setDateFormat(InterfaceTraceabilityTree.CONST_SHORT_DATE_PATTERN);
		dt.setDate(i.getDateProcess());
		result.append(InterfaceTraceabilityTree.PROPERTY_PROCESS_DATEPROCESS_PROPERTYNAME);
		result.append("=\"");
		result.append(dt.getDateAsString());
		result.append("\"");

		// Append the end of the tag
		result.append(InterfaceTraceabilityTree.END_XML_TAG);
		result.append(InterfaceTraceabilityTree.END_LINE);

		// Append the processIn
		if (i.getLIn() != null && i.getLIn().size() > 0) {
			// Append the processIn starting tag
			result.append(prefix + InterfaceTraceabilityTree.TAB);
			result.append(InterfaceTraceabilityTree.START_XML_TAG);
			result.append(InterfaceTraceabilityTree.PROCESS_IN_TAG);
			result.append(InterfaceTraceabilityTree.END_XML_TAG);
			result.append(InterfaceTraceabilityTree.WHITE_SPACE);
			result.append(InterfaceTraceabilityTree.END_LINE);
			
			for (int index = 0; index < i.getLIn().size(); index++) {
				result.append(this.productToXml((ProductInfo)i.getLIn().get(index), prefix + InterfaceTraceabilityTree.TAB + InterfaceTraceabilityTree.TAB));
				result.append(InterfaceTraceabilityTree.END_LINE);
			} // for each processIn
			
			// Append the processIn end tag
			result.append(prefix + InterfaceTraceabilityTree.TAB);
			result.append(InterfaceTraceabilityTree.START_XML_TAG);
			result.append(InterfaceTraceabilityTree.SLASH);
			result.append(InterfaceTraceabilityTree.PROCESS_IN_TAG);
			result.append(InterfaceTraceabilityTree.END_XML_TAG);
			result.append(InterfaceTraceabilityTree.END_LINE);
		} // if exists any processIn
		
		// Append the workers
		if (i.getLWorkers() != null && i.getLWorkers().size() > 0) {
			for (int index = 0; index < i.getLWorkers().size(); index++) {
				result.append(this.workerToXml((WorkerInfo)i.getLWorkers().get(index), prefix + InterfaceTraceabilityTree.TAB));
				result.append(InterfaceTraceabilityTree.END_LINE);
			} // for each worker
		} // if exists any worker

		// Append the workcenters
		if (i.getLWorkcenters() != null && i.getLWorkcenters().size() > 0) {
			for (int index = 0; index < i.getLWorkcenters().size(); index++) {
				result.append(this.workcenterToXml((WorkcenterInfo)i.getLWorkcenters().get(index), prefix + InterfaceTraceabilityTree.TAB));
				result.append(InterfaceTraceabilityTree.END_LINE);
			} // for each workcenter
		} // if exists any workcenter
		
		// Append the comments
		if (i.getLComments() != null && i.getLComments().size() > 0) {
			for (int index = 0; index < i.getLComments().size(); index++) {
				result.append(this.commentToXml((String)i.getLComments().get(index), prefix + InterfaceTraceabilityTree.TAB));
				result.append(InterfaceTraceabilityTree.END_LINE);
			} // for each comment
		} // if exists any comment
		
		// Append the end tag
		result.append(prefix);
		result.append(InterfaceTraceabilityTree.START_XML_TAG);
		result.append(InterfaceTraceabilityTree.SLASH);
		result.append(InterfaceTraceabilityTree.PROCESS_TAG);
		result.append(InterfaceTraceabilityTree.END_XML_TAG);
		
		return result.toString();
	}
	
	/**
	 * Converts the traceability tree worker item to a formatted String
	 * in XML format, that will hold all the item's data.
	 * @param i WorkerInfo, info object linked to the item.
	 * @param prefix String, prefix added to each of the XML lines
	 * resulting from the item conversion.
	 * @return String, formatted String with the worker item's data in XML
	 * format.
	 */
	private String workerToXml(WorkerInfo i, String prefix) {
		StringBuffer result = new StringBuffer();

		// Append worker tag
		result.append(prefix);
		result.append(InterfaceTraceabilityTree.START_XML_TAG);
		result.append(InterfaceTraceabilityTree.WORKER_TAG);
		result.append(InterfaceTraceabilityTree.WHITE_SPACE);
		
		// Append worker code attribute
		result.append(InterfaceTraceabilityTree.PROPERTY_WORKER_CODE_PROPERTYNAME);
		result.append("=\"");
		result.append(i.getCodWorker());
		result.append("\"");
		result.append(InterfaceTraceabilityTree.WHITE_SPACE);
		
		// Append worker name attribute
		result.append(InterfaceTraceabilityTree.PROPERTY_WORKER_NAME_PROPERTYNAME);
		result.append("=\"");
		result.append(i.getDescription());
		result.append("\"");

		// Append the end of the tag
		result.append(InterfaceTraceabilityTree.SLASH);
		result.append(InterfaceTraceabilityTree.END_XML_TAG);
		
		return result.toString();
	}
	
	/**
	 * Converts the traceability tree workcenter item to a formatted String
	 * in XML format, that will hold all the item's data.
	 * @param i WorkcenterInfo, info object linked to the item.
	 * @param prefix String, prefix added to each of the XML lines
	 * resulting from the item conversion.
	 * @return String, formatted String with the workcenter item's data in XML
	 * format.
	 */
	private String workcenterToXml(WorkcenterInfo i, String prefix) {
		StringBuffer result = new StringBuffer();

		// Append worker tag
		result.append(prefix);
		result.append(InterfaceTraceabilityTree.START_XML_TAG);
		result.append(InterfaceTraceabilityTree.WORKCENTER_TAG);
		result.append(InterfaceTraceabilityTree.WHITE_SPACE);
		
		// Append workcenter code attribute
		result.append(InterfaceTraceabilityTree.PROPERTY_WORKCENTER_CODE_PROPERTYNAME);
		result.append("=\"");
		result.append(i.getCodWorkcenter());
		result.append("\"");
		result.append(InterfaceTraceabilityTree.WHITE_SPACE);
		
		// Append workcenter description attribute
		result.append(InterfaceTraceabilityTree.PROPERTY_WORKCENTER_DESCRIPTION_PROPERTYNAME);
		result.append("=\"");
		result.append(i.getDescription());
		result.append("\"");

		// Append the end of the tag
		result.append(InterfaceTraceabilityTree.SLASH);
		result.append(InterfaceTraceabilityTree.END_XML_TAG);
		
		return result.toString();
	}

	/**
	 * Converts the traceability tree warehouse item to a formatted String
	 * in XML format, that will hold all the item's data.
	 * @param i WarehouseInfo, info object linked to the item.
	 * @param prefix String, prefix added to each of the XML lines
	 * resulting from the item conversion.
	 * @return String, formatted String with the warehouse item's data in XML
	 * format.
	 */
	private String warehouseToXml(WarehouseInfo i, String prefix) {
		StringBuffer result = new StringBuffer();

		// Append warehouse tag
		result.append(prefix);
		result.append(InterfaceTraceabilityTree.START_XML_TAG);
		result.append(InterfaceTraceabilityTree.WAREHOUSE_TAG);
		result.append(InterfaceTraceabilityTree.WHITE_SPACE);
		
		// Append warehouse code attribute
		result.append(InterfaceTraceabilityTree.PROPERTY_WAREHOUSE_CODE_PROPERTYNAME);
		result.append("=\"");
		result.append(i.getCodWarehouse());
		result.append("\"");
		result.append(InterfaceTraceabilityTree.WHITE_SPACE);
		
		// Append warehouse description attribute
		result.append(InterfaceTraceabilityTree.PROPERTY_WAREHOUSE_DESCRIPTION_PROPERTYNAME);
		result.append("=\"");
		result.append(i.getDescription());
		result.append("\"");

		// Append the end of the tag
		result.append(InterfaceTraceabilityTree.END_XML_TAG);
		result.append(InterfaceTraceabilityTree.END_LINE);
		
		// Append the appccs
		if (i.getLAppccs() != null && i.getLAppccs().size() > 0) {
			for (int index = 0; index < i.getLAppccs().size(); index++) {
//				result.append(this.appccToXml((AppccInfo)i.getLAppccs().get(index), prefix + InterfaceTraceabilityTree.TAB));
//				result.append(InterfaceTraceabilityTree.END_LINE);
			} // for each appcc
		} // if exists any appcc
		
		// Append the end tag
		result.append(prefix);
		result.append(InterfaceTraceabilityTree.START_XML_TAG);
		result.append(InterfaceTraceabilityTree.SLASH);
		result.append(InterfaceTraceabilityTree.WAREHOUSE_TAG);
		result.append(InterfaceTraceabilityTree.END_XML_TAG);
		
		return result.toString();
	}
	
/*	private String appccToXml(AppccInfo i, String prefix) {
		StringBuffer result = new StringBuffer();

		// Append worker tag
		result.append(prefix);
		result.append(InterfaceTraceabilityTree.START_XML_TAG);
		result.append(InterfaceTraceabilityTree.WORKCENTER_TAG);
		result.append(InterfaceTraceabilityTree.WHITE_SPACE);
		
		// Append workcenter code attribute
		result.append(InterfaceTraceabilityTree.PROPERTY_WORKCENTER_CODE_PROPERTYNAME);
		result.append("=\"");
		result.append(i.getCodWorkcenter());
		result.append("\"");
		result.append(InterfaceTraceabilityTree.WHITE_SPACE);
		
		// Append workcenter description attribute
		result.append(InterfaceTraceabilityTree.PROPERTY_WORKCENTER_DESCRIPTION_PROPERTYNAME);
		result.append("=\"");
		result.append(i.getDescription());
		result.append("\"");

		// Append the end of the tag
		result.append(InterfaceTraceabilityTree.END_XML_TAG);
		
		return result.toString();
	}
*/
	
	/**
	 * Converts the traceability tree comment item to a formatted String
	 * in XML format, that will hold all the item's data.
	 * @param comment String, info object linked to the item.
	 * @param prefix String, prefix added to each of the XML lines
	 * resulting from the item conversion.
	 * @return String, formatted String with the comment item's data in XML
	 * format.
	 */
	private String commentToXml(String comment, String prefix) {
		StringBuffer result = new StringBuffer();

		// Append comment tag
		result.append(prefix);
		result.append(InterfaceTraceabilityTree.START_XML_TAG);
		result.append(InterfaceTraceabilityTree.COMMENT_TAG);
		result.append(InterfaceTraceabilityTree.END_XML_TAG);
		
		// Append comment value
		result.append(comment);
		
		// Append the end of the tag
		result.append(InterfaceTraceabilityTree.START_XML_TAG);
		result.append(InterfaceTraceabilityTree.SLASH);
		result.append(InterfaceTraceabilityTree.COMMENT_TAG);
		result.append(InterfaceTraceabilityTree.END_XML_TAG);
		
		return result.toString();
	}
	
	/**
	 * Returns tag Id assigned to CVS source file.
	 */
	public static String getRevision() {
		return "$Id: TraceabilityTreeItem.java,v 1.3 2009-03-02 15:31:23 mario Exp $";
	}
}
